package com.fpost.form;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;

import com.fpost.constants.ActionConstants;
import com.fpost.form.dto.ArticleTypeDTO;
import com.fpost.util.FpostCache;

public class CreateArticleForm extends ActionForm {

	private String articleId;
	private String title;
	private String authorUsername;
	private String authorFirstName;
	private String authorLastName;
	private String createdOn;
	private String modifiedOn;
	private String keywords;
	private String summary;
	private String body;
	
	private String articleTypeId; // this is what select returns	
	private List<ArticleTypeDTO> articleTypeDTOList;

	public ActionErrors validate(ActionMapping mapping,	HttpServletRequest request) {

		ActionErrors errors = new ActionErrors();
		
		if (title != null && ActionConstants.ZERO.equals(articleTypeId)) {
			errors.add("articleTypeId", new ActionMessage("error.articleTypeId.zero"));
		}		
		if ("".equals(title)) {
			errors.add("title1", new ActionMessage("error.title.empty"));
		}
		if ("".equals(summary)) {
			errors.add("summary1", new ActionMessage("error.summary.empty"));
		}
		if ("".equals(body)) {
			errors.add("body1", new ActionMessage("error.body.empty"));
		}
		
		String badWord = "fuck";
		if (title != null && title.contains(badWord)) {
			errors.add("title2", new ActionMessage("error.title.badwords"));
		}
		if (keywords != null && keywords.contains(badWord)) {
			errors.add("keywords", new ActionMessage("error.keywords.badwords"));
		}
		if (summary != null && summary.contains(badWord)) {
			errors.add("summary2", new ActionMessage("error.summary.badwords"));
		}
		if (body != null && body.contains(badWord)) {
			errors.add("body2", new ActionMessage("error.body.badwords"));
		}

		return errors;
	}

	
	public String getArticleId() {
		return articleId;
	}

	public void setArticleId(String articleId) {
		this.articleId = articleId;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getSummary() {
		return summary;
	}


	public void setSummary(String summary) {
		this.summary = summary;
	}


	public String getBody() {
		return body;
	}

	public void setBody(String body) {
		this.body = body;
	}

	public String getCreatedOn() {
		return createdOn;
	}

	public void setCreatedOn(String createdOn) {
		this.createdOn = createdOn;
	}

	public String getModifiedOn() {
		return modifiedOn;
	}

	public void setModifiedOn(String modifiedOn) {
		this.modifiedOn = modifiedOn;
	}

	public String getKeywords() {
		return keywords;
	}

	public void setKeywords(String keywords) {
		this.keywords = keywords;
	}

	public String getAuthorUsername() {
		return authorUsername;
	}

	public void setAuthorUsername(String authorUsername) {
		this.authorUsername = authorUsername;
	}

	public String getAuthorFirstName() {
		return authorFirstName;
	}

	public void setAuthorFirstName(String authorFirstName) {
		this.authorFirstName = authorFirstName;
	}

	public String getAuthorLastName() {
		return authorLastName;
	}

	public void setAuthorLastName(String authorLastName) {
		this.authorLastName = authorLastName;
	}

	public String getArticleTypeId() {
		return articleTypeId;
	}

	public void setArticleTypeId(String articleTypeId) {
		this.articleTypeId = articleTypeId;
	}

	public List<ArticleTypeDTO> getArticleTypeDTOList() {
		return FpostCache.getInstance().getArticleTypeDTOList();
	}

	public void setArticleTypeDTOList(List<ArticleTypeDTO> articleTypeDTOList) {
		this.articleTypeDTOList = articleTypeDTOList;
	}

}
